package com.example.administrator.ksf_zty.Activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.example.administrator.ksf_zty.R;
import com.example.administrator.ksf_zty.util.BaseActivity;
import com.example.administrator.ksf_zty.util.Config;
import com.example.administrator.ksf_zty.util.SQLiteHelper;
import com.example.administrator.ksf_zty.util.Tips;
import com.example.administrator.ksf_zty.util.TitleUtils;

import static com.example.administrator.ksf_zty.util.SharedPreferencesUtils.setParam;
import static com.example.administrator.ksf_zty.util.Tips.show;

public class SysSetActivity extends BaseActivity {

    ImageButton back;
    Button titleButton;
    private static SQLiteDatabase sqLiteDatabase;
    EditText  SysTxtIP ;
    EditText  SysTxtPort;
    EditText  SysTxtI_WERKS;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sys_set);

        SetTitle();

        SQLiteOpenHelper helper = new SQLiteHelper(SysSetActivity.this, Config.DataBaseName, null, 1);
        sqLiteDatabase = helper.getWritableDatabase();

        SysTxtIP = (EditText) findViewById(R.id.SysTxtIP);
        SysTxtPort = (EditText) findViewById(R.id.SysTxtPort);
        SysTxtI_WERKS = (EditText) findViewById(R.id.SysTxtI_WERKS);

        //Cursor cursor=sqLiteDatabase.query("SysSet",null,null,null,null,null,null);
        Cursor cursor = sqLiteDatabase.rawQuery("select IP,Port,I_WERKS from SysSet ",null);
        if(cursor.moveToFirst())
        {
            SysTxtIP.setText( cursor.getString(cursor.getColumnIndex("IP")));
            SysTxtPort.setText( cursor.getString(cursor.getColumnIndex("Port")));
            SysTxtI_WERKS.setText( cursor.getString(cursor.getColumnIndex("I_WERKS")));
        }
        SetOnClick();
    }

    //给按钮设置事件
    private void SetOnClick()
    {
        //保存按钮
        Button SysSet_btnSave = (Button) findViewById(R.id.SysSet_btnSave);
        SysSet_btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strIp=  SysTxtIP.getText().toString();
                String strPort=  SysTxtPort.getText().toString();
                String strI_WERKS= SysTxtI_WERKS.getText().toString();
                Cursor cursor = sqLiteDatabase.rawQuery("select IP,Port,I_WERKS from SysSet ",null);
                String insertSQL;
                if(cursor.getCount()>0) {
                    insertSQL = " Update SysSet Set IP='"+strIp+"',Port='"+strPort+"',I_WERKS='"+strI_WERKS+"' ";
                }
                else {
                    insertSQL = " insert into SysSet(IP,Port,I_WERKS) values('" + strIp + "','" + strPort + "','" + strI_WERKS + "')";
                }
                //String insertSQL=" Delete from SysSet";
                sqLiteDatabase.execSQL(insertSQL);

                setParam(SysSetActivity.this, "ip", strIp);
                setParam(SysSetActivity.this, "port", strPort);

                show(SysSetActivity.this, "保存成功", 1000);
            }
        });

        //取消按钮
        Button SysSet_btnCanCel = (Button) findViewById(R.id.SysSet_btnCanCel);
        SysSet_btnCanCel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SysSetActivity.this, UserLoginActivity.class);
                intent.putExtra("name", "");//传参数
                startActivity(intent);
            }
        });

    }

    //设置Title
    private void  SetTitle()
    {
        back = (ImageButton) findViewById(R.id.back);
        titleButton = (Button) findViewById(R.id.titleButton);
        //设置标题(第三个参数是标题文字，直接替换""内容即可，第五个参数是标题右边按钮文字，第六个参数是设置是否显示返回按钮)
        TitleUtils.setTitle(this, R.id.title_sys_set, "设置页面", this,
                "", true);
        //返回按钮事件
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
}
